//
//  QInvoiceDetailVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/20.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QInvoiceDetailVC.h"
#import "QInvoiceDetailCell.h"
#import "QCustomerOrderCell.h"
#import "QRecordDetailModel.h"
#import "QOrderModel.h"
#import "QCommonStateVC.h"
#import "QInvoiceListModel.h"
@interface QInvoiceDetailVC ()<UITableViewDelegate,UITableViewDataSource>
{
    QRecordDetailModel *_detailModel;
}
@property (nonatomic,strong)UITableView *tbView;

@end

@implementation QInvoiceDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"开票详情";
    [self createUI];
    [self requestData];
}
- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"findInvoiceRecordDetail"];
    NSDictionary *param = [QParamService findInvoiceRecordDetailWithInvoiceId:self.model.invoiceId];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            _detailModel =[QRecordDetailModel objectFromDict:basModel.items];
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];

}
- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tbView registerNib:[UINib nibWithNibName:@"QInvoiceDetailCell" bundle:nil] forCellReuseIdentifier:@"QInvoiceDetailCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QCustomerOrderCell" bundle:nil] forCellReuseIdentifier:@"QCustomerOrderCell"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        return 50;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section ==1) {
        return 60;
    }
    return 15;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else{
        return _detailModel.list.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        QInvoiceDetailCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QInvoiceDetailCell" forIndexPath:indexPath];
        cell.indexPath =indexPath;
        if (indexPath.row ==0) {
            cell.priceLab.text = _detailModel.invoiceName;
            cell.priceLab.textColor = [QColorUtility defaultBlueColor];
        }else if (indexPath.row == 1){
            cell.priceLab.text = _detailModel.invoiceAmount;
            cell.amoutLab.text = [NSString stringWithFormat:@"元(%@个订单)",_detailModel.orderNum];
        }else if (indexPath.row == 2){
            cell.priceLab.text = _detailModel.invoiceTaxation;
            cell.amoutLab.text = @"元";
        }else if (indexPath.row == 3){
            cell.amoutLab.text = [NSString stringWithFormat:@"%@ %@",_detailModel.userName,_detailModel.userPhone];
        }
        return cell;

    }else{
        QCustomerOrderCell *cell =[tableView dequeueReusableCellWithIdentifier:@"QCustomerOrderCell"];
        QOrderModel *model =_detailModel.list[indexPath.row];
        cell.titleLab.text =[NSString stringWithFormat:@"%@",model.orderType];
        cell.priceLab.text =[NSString stringWithFormat:@"￥%@",model.amount];
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView * header =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        header.backgroundColor =[UIColor whiteColor];
        UILabel *lab =[QViewUtility createLabelWithFrame:CGRectMake(23, 15, 100, 20) font:Ksize15 textAlignment:0 textColor:[QColorUtility defaultBackColor] text:@"开票订单"];
        [header addSubview:lab];
        return header;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 1) {
        UIView * footer =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        footer.backgroundColor =[UIColor whiteColor];
        UILabel *lab1 =[UILabel new];
        lab1.font = [UIFont systemFontOfSize:Ksize15];
        lab1.textColor = [QColorUtility defaultRedColor];
        [footer addSubview:lab1];
        [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo (@-22);
            make.height.equalTo(@20);
            make.top.equalTo(@15);
        }];
        
        lab1.text = [NSString stringWithFormat:@"%@元",_detailModel.invoiceAmount];
        
        UILabel *lab =[UILabel new];
        [footer addSubview:lab];
        lab.font = [UIFont systemFontOfSize:Ksize16];
        lab.textColor = [QColorUtility defaultBackColor];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo (@20);
            make.top.equalTo(@15);
            make.right.mas_equalTo(lab1.mas_left).offset(-8);
        }];
        lab.text =@"合计:";
        
        UIView *line =[UIView new];
        [footer addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo (@20);
            make.right.equalTo (@-20);
            make.top.equalTo (@0);
            make.height.equalTo(@.7);
        }];
        line.backgroundColor =[QColorUtility defaultLineColor];
        
        
        
        return footer;

    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QCommonStateVC * vc =[[QCommonStateVC alloc]init];
    vc.credentialsId = _detailModel.credentialsId;
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
